﻿//using System;
//using System.Collections.Generic;
//using System.IO;
//using System.Linq;
//using System.Runtime.Serialization;
//using System.Runtime.Serialization.Formatters.Binary;
//using System.Text;

//namespace zielonka.co.uk.extensions.system.io
//{
//    // Description:	These 2 extension method allows you to delete a folder or just purge all the folder content even if there are Readonly, System, and/or Hidden attributes files in it. The default Delete method doesn't work if there are files with Readonly, System, and/or Hidden attributes.
//    /*
//        directoryToDelete.DeleteWithPrejudice();
//     */

//    //TODO:This code needs extra work.
//    public static partial class DirectoryInfoExtensions
//    {
//        public static void DeleteWithPrejudice(this DirectoryInfo directoryInfo)
//        {
//            directoryInfo.DeleteWithPrejudice(true);
//        }

//        public static void Purge(this DirectoryInfo directoryInfo)
//        {
//            directoryInfo.DeleteWithPrejudice(false);
//        }

//        private static void DeleteWithPrejudice(this DirectoryInfo directoryInfo, bool deleteRoot)
//        {
//            /* Determine whether the directory exists */
//            if (!directoryInfo.RefreshExists())
//            {
//                return;
//            }
//            directoryInfo.Refresh();
//            /* Remove Readonly, System, and Hidden attributes prior to deletion */
//            directoryInfo.Attributes = FileAttributes.Directory;

//            directoryInfo.DeleteAllFiles(true);

//            var folderPaths = new List<DirectoryInfo>();

//            foreach (var subDirectory in directoryInfo.GetDirectories("*", SearchOption.AllDirectories))
//            {
//                folderPaths.Add(subDirectory);
//            }
//            if (deleteRoot)
//            {
//                folderPaths.Add(directoryInfo);
//            }

//            // sort has a side effect that means the deepest directories will be at the top
//            folderPaths.Sort((x, y) => y.FullName.CompareTo(x.FullName));

//            foreach (var folderPath in folderPaths)
//            {
//                if (!folderPath.RefreshExists())
//                {
//                    continue;
//                }
//                folderPath.Attributes = directoryInfo.Attributes & ~ReadOnlyHiddenSystemFileAttributes;
//                folderPath.Refresh();
//                folderPath.Delete();
//            }
//        }
//    }
//}
